//
//  HYMyCollectionViewModel.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/21.
//

#import "HYMyCollectionViewModel.h"
#import "HYHomeFileItemViewModel.h"
#import "HYMyCollectionModel.h"
#import "HYVideoModel.h"
#import "HYVideoPlayerConfig.h"
#import "HYVideoViewModel.h"


@interface HYMyCollectionViewModel ()

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSMutableArray *collectSource;

@property (nonatomic, strong) NSMutableArray *videoSource;

@property (nonatomic, strong) NSMutableArray *audioSource;

@end

@implementation HYMyCollectionViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        self.playSubject = [RACSubject subject];
    }
    return self;
}

- (void)loadData
{
    //在视频播放器里面取消收藏后重新获取数据
    [self removeData];
    self.collectSource = [HYMyCollectionModel fetchAllCollectRecord];
    [self.collectSource enumerateObjectsUsingBlock:^(HYMyCollectionModel *  _Nonnull collectModel, NSUInteger idx, BOOL * _Nonnull stop) {
        HYHomeFileItemViewModel *itemVM = [[HYHomeFileItemViewModel alloc]initWitCollectModel:collectModel];
        HYVideoModel *videoModel = [self fetchCurrentVideoWithItemViewModel:itemVM];
        if(itemVM.fileType == HYHomeFileType_Video) {
            [self.videoSource addObject:videoModel];
        }else{
            [self.audioSource addObject:videoModel];
        }
        [self.dataSource addObject:itemVM];
    }];
    [self.updatedContentSignal sendNext:self.collectSource];
}
- (void)removeData
{
    [self.collectSource removeAllObjects];
    [self.videoSource removeAllObjects];
    [self.audioSource removeAllObjects];
    [self.dataSource removeAllObjects];
}
- (HYVideoModel *)fetchCurrentVideoWithItemViewModel:(HYHomeFileItemViewModel *)itemViewModel
{
    HYVideoModel *videoModel = [[HYVideoModel alloc]init];
    videoModel.videoTitle = itemViewModel.fileName;
    videoModel.videoUrl = itemViewModel.filePath;
    videoModel.videoImage = itemViewModel.videoImage;
    videoModel.videoImagePath = itemViewModel.fileImage;
    videoModel.isAudio = itemViewModel.fileType == HYHomeFileType_Video ? NO : YES;
    videoModel.createDate = itemViewModel.createDate;
    videoModel.importDate = itemViewModel.importDate;
    videoModel.videoDuration = itemViewModel.videoDuration;
    videoModel.isPrivate = itemViewModel.isSuperPrivate;
    return videoModel;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (id)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataSource[indexPath.row];
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYHomeFileItemViewModel *itemViewModel = self.dataSource[indexPath.row];
    HYVideoModel *currentVideo = [self fetchCurrentVideoWithItemViewModel:itemViewModel];
    if([itemViewModel.folderIdentification isEqualToString:@"fileImport-vplayer"]) {
        BOOL isExist = [kFileManager fileExistsAtPath:itemViewModel.filePath];
        if(!isExist){
            [HYHub showMessage:NSLocalizedString(@"文件不存在,建议取消收藏", nil)];
            return;
        }
    }
    currentVideo.isSelect = YES;
    if (currentVideo.isAudio) {
        [self.audioSource enumerateObjectsUsingBlock:^(HYVideoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.videoDuration == currentVideo.videoDuration && obj.createDate == currentVideo.createDate) {
                obj.isSelect = YES;
            }
        }];
    }else{
        //设置选中的属性
        [self.videoSource enumerateObjectsUsingBlock:^(HYVideoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.videoDuration == currentVideo.videoDuration && obj.createDate == currentVideo.createDate) {
                obj.isSelect = YES;
            }
        }];
    }
    HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
    config.isBasePlayer = YES;
    config.currentVideoModel = currentVideo;
    config.isAudioMode = currentVideo.isAudio ? YES : NO;
    config.videoSoucres = currentVideo.isAudio ? self.audioSource : self.videoSource;
    HYVideoViewModel *videoModel = [[HYVideoViewModel alloc]initWithFolderIdentification:itemViewModel.folderIdentification isPrivate:itemViewModel.isSuperPrivate videoConfig:config];
    [self.playSubject sendNext:RACTuplePack(videoModel, currentVideo.isAudio ? @"1" : @"0")];
    
}

- (BOOL)deleteCollectAtIndexPath:(NSIndexPath *)indexPath
{
    HYMyCollectionModel *model = self.collectSource[indexPath.row];
    BOOL isDeleted =  [model deleteToDB];
    if (isDeleted) {
        __block NSUInteger index;
        if (model.fileType == HYHomeFileType_Audio) {
            [self.audioSource enumerateObjectsUsingBlock:^(HYVideoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.videoDuration == model.videoDuration && obj.createDate == model.createDate) {
                    index = idx;
                    *stop = YES;
                }
            }];
            [self.audioSource removeObjectAtIndex:index];
        }else{
            [self.videoSource enumerateObjectsUsingBlock:^(HYVideoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.videoDuration == model.videoDuration && obj.createDate == model.createDate) {
                    index = idx;
                    *stop = YES;
                }
            }];
            [self.videoSource removeObjectAtIndex:index];
        }
        [self.dataSource removeObjectAtIndex:indexPath.row];
    }
    return isDeleted;
}

- (void)clearAllCollectionHistory
{
    BOOL result = [HYMyCollectionModel clearAllCollectionHistory];
    if(result){
        [self removeData];
        [self.updatedContentSignal sendNext:self.dataSource];
    }
}

#pragma mark - loadLoazy
- (NSMutableArray *)dataSource
{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)videoSource
{
    if(!_videoSource){
        _videoSource = [NSMutableArray array];
    }
    return _videoSource;
}
- (NSMutableArray *)audioSource
{
    if(!_audioSource){
        _audioSource = [NSMutableArray array];
    }
    return _audioSource;
}

@end
